/*!
 * @author zhangzhenfu
 * Copyright 2020/3/12 9:42 projects.com
 */
//tab
;(function(win){
    var Tab = win.Tab = {},
        Message = win.Message = {};
    var $tabContent = $("#content");

    Tab.init = function(){
        $tabContent.ligerTab();
        Tab.M = $tabContent.ligerGetTabManager();
        Tab.bind();
        return this;
    };
    //绑定菜单
    Tab.bind = function(){
        if(Tab.M){
            $("a[data-tab='true']").on("click",function(e){
                e.preventDefault();
                var tabid = $(this).data("tabid"),title = $(this).attr("title")?$(this).attr("title"):"未定义",url = $(this).attr("href");
                if(tabid){
                    if(Tab.M.isTabItemExist(tabid)){
                        Tab.M.selectTabItem(tabid);
                    } else {
                        if(Tab.M.getTabItemCount()<=10) {
                            Tab.M.addTabItem({url: url,text:title,tabid:tabid});
                        } else {
                            layer.msg("最多只能打开10个窗口哦！");
                        }
                    }

                } else {
                    alert("请定义唯一tabid");
                }
            });
        }
    };
    //页面弹出tab页
    Tab.open = function(options){
        if(Tab.M){
            if(options.tabid==undefined||options.title==undefined||options.url==undefined){
                alert("请定义唯一tabid");
                return;
            }
            if(Tab.M.isTabItemExist(options.tabid)){
                Tab.M.selectTabItem(options.tabid);
                Tab.M.setTabItemTitle(options.tabid,options.title);
                Tab.M.setTabItemSrc(options.tabid,options.url);
                Tab.M.reload(options.tabid);
            }else{
                if(Tab.M.getTabItemCount()<=10) {
                    Tab.M.addTabItem({url: options.url,text:options.title,tabid:options.tabid});
                } else {
                    layer.msg("最多只能打开10个窗口哦！");
                }
            }
        }
    };
    //刷新当前tab的title和url
    Tab.change = function(options){
        if(Tab.M){
            if(options.title==undefined||options.url==undefined){
                alert("请定义title");
                return;
            }
            Tab.M.setTabItemSrc(Tab.M.getSelectedTabItemID(),options.url);
            Tab.M.setTabItemTitle(Tab.M.getSelectedTabItemID(),options.title);
            Tab.M.reload(Tab.M.getSelectedTabItemID());
        }
    };

    //刷新指定tabId
    Tab.reload = function(tabItemId){
        if(Tab.M){
            if(Tab.M.isTabItemExist(tabItemId)){
                Tab.M.reload(tabItemId);
            }
        }
    };

    return win.Tab;
})(window).init();
// message
;(function(win){
    var Message = win.Message = {};
    Message.init = function(url){
        var ws = this.ws = new WebSocket(url);
        ws.onerror = function () {
            console.log("WebSocket连接发生错误");
        };
        //连接成功建立的回调方法
        ws.onopen = function () {
            console.log("WebSocket连接成功");
        };
        //接收到消息的回调方法
        ws.onmessage = function (event) {
            console.log(event.data);
            // toastr.info('你有新消息了！')
        };
        //连接关闭的回调方法
        ws.onclose = function () {
            console.log("WebSocket连接关闭");
        };
        return this;
    };
    Message.send = function (message) {
        if(this.ws){
            this.ws.send(message);
        }else{
            console.log("web socket 未初始化");
        }
    };
    return win.Message;
})(window).init("ws://localhost:8080/admin/webSocket/1/zzf");
